﻿//https://leetcode.cn/problems/longest-consecutive-sequence/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> hash;
        for (auto& e : nums) {
            hash.insert(e);
        }

        int ret = 0, pos = -1;
        int cur = 0;
        for (auto& e : hash)
        {
            // 从当前第一个可连续的位置开始不断枚举e+1 e+2 e+3 直到不连续
            // 再向下一个位置进行枚举
            if (!hash.count(e - 1))
            {
                pos = e;
                cur = 1;
                while (hash.count(pos + 1))
                {
                    pos = pos + 1;
                    cur++;
                }
            }

            ret = max(ret, cur);
        }

        return ret;
    }
};